package com.gsn.desktop.engine;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;

public class ActorUtility {

	public static Vector2 getCoordinate(Actor actor, float x, float y, Group target) {		
		Group tmp = actor.parent;
		float tX = x + actor.x;
		float tY = y + actor.y;
		while (tmp != target){
			tX += tmp.x;
			tY += tmp.y;
			tmp = tmp.parent;
		}
		return new Vector2(tX, tY);
	}
	
	public static void setCenter(Actor actor, float x, float y) {
		setRatio(actor, 0.5f, 0.5f, x, y);
	}

	public static void setRatio(Actor actor, float ratioWidth, float ratioHeight, float x, float y) {
		actor.x = x - actor.width * actor.scaleX * ratioWidth;
		actor.y = y - actor.height * actor.scaleY * ratioHeight;
	}

	public static String getShortName(String name) {
		if (name.length() >= 8)
			return (name.substring(0, 6) + "..");
		else
			return name;
	}
	
	public static String getShortDefineName(String name, int length)
	{
		if (name.length() >= length)
			return (name.substring(0, length - 2) + "..");
		else
			return name;
	}
	
	public static String formatNumber(long value){
		String s = String.valueOf(value);
		String kq = "";
		int count = 0;
		for (int i = s.length() - 1; i >= 0; i--){
			count++;
			kq = (char)s.charAt(i) + kq;
			if (count == 3 && i != 0){
				count = 0;
				kq = "." + kq;
			}
		}
			
		return kq;		
	}
	
	public static boolean containActor(Group group, Actor actor){		
		return group.getActors().contains(actor);
	}
	
	public static Color colorFromHex(long hex)
    {
            float a = (hex & 0xFF000000L) >> 24;
            float r = (hex & 0xFF0000L) >> 16;
            float g = (hex & 0xFF00L) >> 8;
            float b = (hex & 0xFFL);
                            
            return new Color(r/255f, g/255f, b/255f, a/255f);
    }

   
     // Expects a hex value as String and returns the appropriate Color object
     // @param s The hex string to create the Color object from
     // @return
    
    public static Color colorFromHexString(String s)
    {               
            if(s.startsWith("0x"))
                    s = s.substring(2);
            
            if(s.length() != 8) // AARRGGBB
                    throw new IllegalArgumentException("String must have the form AARRGGBB");
                    
            return colorFromHex(Long.parseLong(s, 16));
    }

	
	public static void main(String[] args) {
		System.out.println(formatNumber(212122));
	}
	
	public static String shortString(int length, String s)
	{
		String result = "";
		if(s.length() > length)
		{
			int i;
			for(i=0; i< length - 1; i++)
				result = result + s.charAt(i);
			result = result + "..";
			return result;
		}
		return s;
	}
}
